local Msg = require("core.Msg")
local CritDrawExcel = require("excel.DateActivity").critDraw
local Grid = require("bag.Grid")
local ActDefine = require("activity.Define")
local BagLogic = require("bag.BagLogic")
local Broadcast = require("broadcast.Broadcast")
local Lang = require("common.Lang")
local MailLogicFL = require("mail.MailLogicFL")
local CowryBagLogic = require("fuli.CowryBagLogic")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local ItemDefine = require("bag.ItemDefine")
local ObjHuman = require("core.ObjHuman")
local DateActFL = require("activity.DateActFL")
local HtmlUtil = require("common.HtmlUtil")
local ShopHelp = require("shop.ShopHelp")
local Log = require("common.Log")

function open(human, id, type)
  local sendMsg = Msg.gc.GC_OPEN_DATE_ACTIVITY
  local activityNum = DateActFL.onGetCurActRuleID(id)
  local activity = CritDrawExcel[activityNum]
  if not activity then
    return
  end
  local drawTimes = human.db.DateCritDrawTimes + 1 <= activity.maxDraw and human.db.DateCritDrawTimes + 1 or activity.maxDraw
  sendMsg.info[0] = #activity.weight[drawTimes]  
  local cache = {}
  for i=1, sendMsg.info[0] do
    sendMsg.info[i].reward[0] = 1 --#activity.reward[drawTimes][i]
    Grid.create(cache, activity.reward[drawTimes][i][1], activity.reward[drawTimes][i][2])
    Grid.makeItem(cache, sendMsg.info[i].reward[1], 0, human) 
    sendMsg.info[i].args[0] = 0
    sendMsg.info[i].canGet = 0
    sendMsg.info[i].name = ""
  end
  sendMsg.myInfo[0] = 2
  sendMsg.myInfo[1] = activity.maxDraw - human.db.DateCritDrawTimes
  sendMsg.myInfo[2] = activity.canDraw[drawTimes]
  sendMsg.activityId = id  
  sendMsg.type = type or 1
  Msg.send(sendMsg, human.fd)
  local info = {}
  info.type = id
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_DATE_RECORD, info)
end

function startDraw(human, id)
  local activityNum = DateActFL.onGetCurActRuleID(id)
  local activity = CritDrawExcel[activityNum]
  if not activity then
    return
  end
  local realCnt = activity.maxDraw - human.db.DateCritDrawTimes
  if realCnt <= 0 then
    return
  end
  local drawTimes = human.db.DateCritDrawTimes + 1 <= activity.maxDraw and human.db.DateCritDrawTimes + 1 or activity.maxDraw
  if ObjHuman.getYuanbao(human) < activity.canDraw[drawTimes] then
    return ShopHelp.onNoRMB(human)
  end
  if activity.canDraw[drawTimes] > 0 then
    ObjHuman.decYuanbao(human, activity.canDraw[drawTimes], "dateactholidaygift")
  end
  human.db.DateCritDrawIndex = 0
  local sendMsg = Msg.gc.GC_DATE_START_DRAW
  local weight = 0
  
  for i=1, #activity.weight[drawTimes] do
    weight = weight + activity.weight[drawTimes][i]
  end
  local rand = math.random(1, weight)
  weight = 0
  sendMsg.multi = 0
  for i=1, #activity.weight[drawTimes] do
    weight = weight + activity.weight[drawTimes][i]
    if rand <= weight then
      sendMsg.index = i      
      human.db.DateCritDrawIndex = i
      break
    end
  end
  sendMsg.id = id
  Msg.send(sendMsg, human.fd)
  human.db.DateCritDrawTimes = human.db.DateCritDrawTimes + 1
  
  local drawTimes = human.db.DateCritDrawTimes
  local total = activity.reward[drawTimes][human.db.DateCritDrawIndex][2]
  if activity.reward[drawTimes][human.db.DateCritDrawIndex][1] == ItemDefine.ITEM_ID_YUANBAO then
    ObjHuman.addYuanbao(human, total, "dateactholidaygift")
  elseif activity.reward[drawTimes][human.db.DateCritDrawIndex][1] == ItemDefine.ITEM_ID_YINLIANG then
    ObjHuman.addYinliang(human, total, "dateactholidaygift")
  else
    ObjHuman.addBindYuanbao(human, total, "dateactholidaygift")
  end

--  open(human, ActDefine.OPERATEACT_TYPE_CRITDRAW)
  local info = {}
  info.type = ActDefine.OPERATEACT_TYPE_CRITDRAW
  info.id = activity.reward[drawTimes][human.db.DateCritDrawIndex][1] 
  info.cnt = activity.reward[drawTimes][human.db.DateCritDrawIndex][2]
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_DATE_DRAW, info)
  local cache = {}
  Grid.create(cache, info.id, info.cnt)
  local str = string.format(Lang.DATE_CRIT_DRAW_BRO, HtmlUtil.fontName(human.name, human._id), HtmlUtil.fontItem(cache), human._id)
  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
  human.db.DateCritDrawIndex = 0  
  Log.write(Log.LOGID_OSS_YUNYING_CRIT_DRAW, human._id, human.account, human.name,human.db.lv, human.db.DateCritDrawTimes, info.id, info.cnt)
end

function endDraw(human)
  open(human, ActDefine.OPERATEACT_TYPE_CRITDRAW)
--  if human.db.DateCritDrawIndex == 0 then
--    return
--  end
--  
--  local activityNum = DateActFL.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_CRITDRAW)
--  local activity = CritDrawExcel[activityNum]
--  if not activity then
--    return
--  end
--  local realCnt = activity.maxDraw - human.db.DateCritDrawTimes + 1
--  if realCnt <= 0 then
--    return
--  end
--  local drawTimes = human.db.DateCritDrawTimes
--  local total = activity.reward[drawTimes][human.db.DateCritDrawIndex][2]
--  if activity.reward[drawTimes][human.db.DateCritDrawIndex][1] == ItemDefine.ITEM_ID_YUANBAO then
--    ObjHuman.addYuanbao(human, total, "dateactholidaygift")
--  elseif activity.reward[drawTimes][human.db.DateCritDrawIndex][1] == ItemDefine.ITEM_ID_YINLIANG then
--    ObjHuman.addYinliang(human, total, "dateactholidaygift")
--  else
--    ObjHuman.addBindYuanbao(human, total, "dateactholidaygift")
--  end
--
--  open(human, ActDefine.OPERATEACT_TYPE_CRITDRAW)
--  local info = {}
--  info.type = ActDefine.OPERATEACT_TYPE_CRITDRAW
--  info.id = activity.reward[drawTimes][human.db.DateCritDrawIndex][1] 
--  info.cnt = activity.reward[drawTimes][human.db.DateCritDrawIndex][2]
--  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_DATE_DRAW, info)
--  local cache = {}
--  Grid.create(cache, info.id, info.cnt)
--  local str = string.format(Lang.YUNYING_CRIT_DRAW_BRO, HtmlUtil.fontName(human.name, human._id), HtmlUtil.fontItem(cache), human._id)
--  Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
--  human.db.DateCritDrawIndex = 0
--  open(human, ActDefine.OPERATEACT_TYPE_CRITDRAW)
--  Log.write(Log.LOGID_OSS_YUNYING_CRIT_DRAW, human._id, human.account, human.name,human.db.lv, human.db.DateCritDrawTimes, info.id, info.cnt)
end


